package com.tensquare.base.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class LabelService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LabelDao labelDao;

    public void save(Label label) {
        label.setId(idWorker.nextId()+"");
        label.setFans(0L);
        System.out.println(label);
        labelDao.insert(label);
    }

    public List<Label> findAll() {
       return labelDao.selectList(null);

    }

    public List<Label> findByToplist() {
        Map<String,Object> map=new HashMap<>();
        map.put("recommend","1");
        List<Label> byrecommend = labelDao.selectByMap(map);
        return byrecommend;
    }

    public List<Label> findByList() {
        Map<String,Object> map=new HashMap<>();
        map.put("state","1");
        List<Label> byrecommend = labelDao.selectByMap(map);
        return byrecommend;
    }

    public Label findById(String labelId) {
        return labelDao.selectById(labelId);
    }

    public void updateByLabelId(Label label) {
        labelDao.updateById(label);
    }

    public void deleteByLabelId(String labelId) {
        labelDao.deleteById(labelId);
    }

    public Page<Label> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<Label> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        Page<Label> page1=new Page<>(page,size);
        List<Label> labels = labelDao.selectPage(page1, wrapper);
        page1.setRecords(labels);
        return page1;
    }

    public List<Label> findBySearch(Map<String, Object> map) {
        return labelDao.selectByMap(map);
    }
}
